/*
 *  Copyright 2004 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *  
 */
/* August 2004 */
/**************************************************************************/
/* tvp51xx.c file */
/**************************************************************************/
#include <std.h>
#include <csl_i2c.h>      
#include <csl_stdinc.h>
#include <edc.h>
#include <tvp51xx.h>
                           

#include "_iic.h"
#include "_tvp51xx.h"
#define	NTSC_VL	525
#define PAL_VL	625
#define AutoDetect
typedef enum {INTERLACED = TRUE, PROGRESSIVE = FALSE} FrameType ;   

static void configTVP5150(Arg arg);
static void configTVP5146(Arg arg);
static void powerdownTVP51XX(Int channel, Bool powerDownEnable);
static void resetTVP51XX(int devId);
static void setTVP5146AMuxMode(int devId, int Arg);
static void setTVP5150AMuxMode(int devId, int Arg);
static Int TVP51XX_ctrl(EDC_Handle handle, TVP51XX_Cmd cmd, Arg arg);
static Int TVP51XX_close(EDC_Handle handle);
static EDC_Handle TVP51XX_open(String devName, Arg optArg);



EDC_Fxns TVP51XX_Fxns = {
    TVP51XX_open,
    TVP51XX_close,
    TVP51XX_ctrl                   
};

static I2C_Handle hI2C[2] = {INV, INV};

/*
 * ======== configTVP5150 ========
 */
static void configTVP5150(Arg arg)
{
    Uint8 addrI2C=_TVP5150_IIC_ADDR;
	Uint8 misc_ctrl = 0x6D;
	Uint8 output_format = 0x07;
	Uint8 input_sel = 0x00;	// for cvbs port1
	Uint8 pin_cfg = 0x00;
	Uint8 chro_ctrl_2 = 0x14;
	Uint8 nFandVMode = 0x15;

	
    TVP51XX_ConfParams *TVP51XXParams = (TVP51XX_ConfParams *)arg;
		
#ifdef	AutoDetect	
	Uint16 verticalline = NTSC_VL;
	Uint8 temp;
	
	_IIC_read(TVP51XXParams->hI2C, addrI2C, 0x84, &temp, 1);
	verticalline = temp;
	_IIC_read(TVP51XXParams->hI2C, addrI2C, 0x85, &temp, 1);
	verticalline = verticalline|(temp<<8);
	
	
	if(verticalline == NTSC_VL)
	{
		TVP51XXParams->videoMode = TVP51XX_MODE_NTSCSqp;
	}
	else
	{
		TVP51XXParams->videoMode = TVP51XX_MODE_PALSqp;
	};
#endif
	
    if(TVP51XXParams->videoMode == TVP51XX_MODE_PAL601 || TVP51XXParams->videoMode == TVP51XX_MODE_PALSqp)
    {
    	chro_ctrl_2 = 0x54;
    }

	if(TVP51XXParams->inputPort)
		input_sel = 0x02;	//CVBS port2
	else
		input_sel = 0x0;	//CVBS port1

    _IIC_write(TVP51XXParams->hI2C, addrI2C,0x00, &input_sel,0x1);
    _IIC_write(TVP51XXParams->hI2C, addrI2C,0x03, &misc_ctrl,0x1);
    _IIC_write(TVP51XXParams->hI2C, addrI2C,0x0D, &output_format,0x1);
    _IIC_write(TVP51XXParams->hI2C, addrI2C,0x0F, &pin_cfg,0x1);
    _IIC_write(TVP51XXParams->hI2C, addrI2C,0x1B, &chro_ctrl_2,0x1);

	//Set F and V bits mode to "Toggle F Bit Always"
	_IIC_write(TVP51XXParams->hI2C, addrI2C,0x15, &nFandVMode, 1);

}

/*
 * ======== configTVP5146 ========
 */
static void configTVP5146(Arg arg)
{
    TVP51XX_ConfParams *TVP51XXParams = (TVP51XX_ConfParams *)arg;
    Uint8 addrI2C = _TVP5146_IIC_ADDR;

	Uint8 input_sel = 0x05;
	Uint8 output2 = 0x11;
	Uint8 output3 = 0xFE;
	Uint8 output4 = 0xFF;
	Uint8 output1 = 0x40;
	
	Uint8 nFandVMode = 0x16;

	

#ifdef	AutoDetect	
	Uint16 verticalline = NTSC_VL;
	Uint8 *externalsyn;
	Uint8 temp;
	int	i;
	
	_IIC_read(TVP51XXParams->hI2C, addrI2C, 0x42, &temp, 1);
	verticalline = temp;
	_IIC_read(TVP51XXParams->hI2C, addrI2C, 0x43, &temp, 1);
	verticalline = verticalline|(temp<<8);
	
	
	if(verticalline == NTSC_VL)
	{
		if(TVP51XXParams->videoMode == TVP51XX_MODE_NTSC601)
			externalsyn = (Uint8 *)NTSC601_EXEC;
		else
		{
			TVP51XXParams->videoMode = TVP51XX_MODE_NTSCSqp;
			externalsyn = (Uint8 *)NTSCSGP_EXEC;
		}
			
	}
	else
	{
		if(TVP51XXParams->videoMode == TVP51XX_MODE_PAL601)
			externalsyn = (Uint8 *)PAL601_EXEC;
		else
			{
				TVP51XXParams->videoMode = TVP51XX_MODE_PALSqp;
				externalsyn = (Uint8 *)PALSGP_EXEC;
			}
	};
	if (!TVP51XXParams->enableBT656Sync)
	{
		output1 = 0x43;//10bit YCbCr with separate syn
    	output4 = 0xAF;
    	temp = 0x16;
    	for(i =0; i<16; i++)
    	_IIC_write(TVP51XXParams->hI2C, addrI2C, temp++, externalsyn++, 1);
	}

#else
	Uint8 temp	=	0x16;
	Uint8 *externalsyn;
	Uint8 ext1;
	int	i;

	switch(TVP51XXParams->videoMode)
	{
		case	TVP51XX_MODE_NTSC601:
			externalsyn = (Uint8 *)NTSC601_EXEC;
			break;
		case	TVP51XX_MODE_NTSCSqp:
			externalsyn = (Uint8 *)NTSCSGP_EXEC;
			break;
		case	TVP51XX_MODE_PAL601:
			externalsyn = (Uint8 *)PAL601_EXEC;
			break;
		case	TVP51XX_MODE_PALSqp:
			externalsyn = (Uint8 *)PALSGP_EXEC;
			break;
	};
	
	if (!TVP51XXParams->enableBT656Sync)
	{
		output1 = 0x43;//10bit YCbCr with separate syn
    	output4 = 0xAF;
    	
    	for(i =0; i<16; i++)    	
    	_IIC_write(TVP51XXParams->hI2C, addrI2C, temp++, externalsyn++, 1);
    	
	}
	
#endif
    switch(TVP51XXParams->aFmt)
    {
		case TVP51XX_AFMT_COMPOSITE:
			input_sel = 0x05;
			break;
		case TVP51XX_AFMT_SVIDEO:
			input_sel = 0x46;
			break;
		case TVP51XX_AFMT_RGB:
			input_sel = 0x85;
			break;
		case TVP51XX_AFMT_YPBPR:
			input_sel = 0x95;
			break;
	}
	_IIC_write(TVP51XXParams->hI2C, addrI2C,0x00, &input_sel,0x1);	
	_IIC_write(TVP51XXParams->hI2C, addrI2C,0x33, &output1,0x1);
    _IIC_write(TVP51XXParams->hI2C, addrI2C,0x34, &output2,0x1);
    _IIC_write(TVP51XXParams->hI2C, addrI2C,0x35, &output3,0x1);
	_IIC_write(TVP51XXParams->hI2C, addrI2C,0x36, &output4,0x1);

	//Set F and V bits mode to "Toggle F Bit Always"
	_IIC_write(TVP51XXParams->hI2C, addrI2C,0x75, &nFandVMode, 1);

}



/*
 * ======== powerdownTVP51XX ========
 */
static void powerdownTVP51XX(int devId, Bool powerDownEnable)
{
    Uint8 powerdownSettings = 0x01;


    /*Put _TVP51XX in power down mode */
    if(!powerDownEnable) {
        powerdownSettings = 0x00;
    }
    if (devId)
    	_IIC_write(hI2C[devId], _TVP5150_IIC_ADDR,0x02,&powerdownSettings,1);
    else
    	_IIC_write(hI2C[devId], _TVP5146_IIC_ADDR,0x03,&powerdownSettings,1);
}

/*
 * ======== resetTVP5150A ========
 */
static void resetTVP51XX(int devId)
{
/***************************************************************/
/* Reset 51xx */
/***************************************************************/
    powerdownTVP51XX(devId, FALSE);
}

static void setTVP5150AMuxMode(int devId, int arg)
{
	Uint8 input_sel = (Uint8)arg * 2;	

    _IIC_write(hI2C[devId], _TVP5150_IIC_ADDR, 0x00, &input_sel,0x1);
}

static void setTVP5146AMuxMode(int devId, int arg)
{
	Uint8 input_sel;
	
	if(arg == 1){	/* composite */
		input_sel = 0x05; 
    }
	else if(arg == 0) { /* s-video */
		input_sel = 0x46;
	} else {
	    return;
	}
	_IIC_write(hI2C[devId], _TVP5146_IIC_ADDR,0x00, &input_sel,0x1);	
}



/*
 * ======== TVP51XX_close ========
 */
static Int TVP51XX_close(EDC_Handle edcHandle) 
{
	

    /*First Check if the Handle is correct */
	if((int)edcHandle > 0x1 || (int)edcHandle < 0)
	{
        return EDC_FAILED;
    }
    else
    {
        /*Put _TVP51XX in power down mode */
        powerdownTVP51XX((int)edcHandle, TRUE);
        return EDC_SUCCESS;    
    }
}

/*
 * ======== TVP51XX_ctrl ========
 */
static Int TVP51XX_ctrl(EDC_Handle edcHandle, TVP51XX_Cmd cmd, Arg arg)
{
    Int devId = (int)edcHandle;
    
    switch(cmd) 
    {
        case EDC_CONFIG:  
        {
            hI2C[devId] = ((TVP51XX_ConfParams *)arg)->hI2C;

			if(devId) 
				configTVP5150(arg);
            else
            	configTVP5146(arg);
            
        	break;
        }
        case EDC_RESET:
        	resetTVP51XX(devId);
        	break;
        case TVP51XX_POWERDOWN:
        	powerdownTVP51XX(devId, TRUE);
        	break;                 
        case TVP51XX_POWERUP:
        	powerdownTVP51XX(devId, FALSE);
        	break;                         
        case TVP51XX_SET_AMUXMODE:
			if(devId) 
				setTVP5150AMuxMode(devId, arg);
            else
            	setTVP5146AMuxMode(devId, arg);
        	break;
    }                          
    return EDC_SUCCESS;
}

 
/*
 * ======== TVP51XX_open ========
 */
static EDC_Handle TVP51XX_open(String devName, Arg optArg)
{
    int handle;

    /* Check if the addresse is correct */
    if(devName[0] != '/') 
    {
        handle = -1;
    }
    else
    {
    	handle = (int)(devName[1] - '0');
    }
    
    if (handle < 0||handle >1)
    {
    	return (EDC_Handle)EDC_FAILED;
    }
    
   return (EDC_Handle)handle;
}


/**************************************************************************/
/* End of file                                                                            */
/**************************************************************************/

